package com.ss.android.newmedia.e;

import android.content.Context;
import android.os.Message;

import com.bytedance.common.utility.UIUtils;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.ss.android.account.Spipe;
import com.ss.android.account.model.SharePlatform;
import com.ss.android.action.a.a.Comment;
import com.ss.android.article.browser.R;
import com.ss.android.common.app.IComponent;
import com.ss.android.common.dialog.AlertDialog;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.model.AbsArticle;
import com.ss.android.newmedia.BaseAppData;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public final class ShareHelper implements Callback {
    public final Context mContext;
    public final BaseAppData mBaseAppData;
    public final Spipe mSpipe;
    private IComponent component;
    private SharePlatform[] mSharePlatforms;
    private WeakHandler mWeakHandler;

    public ShareHelper(Context context, IComponent component, BaseAppData baseAppData) {
        this.mWeakHandler = new WeakHandler(((Callback) this));
        this.mContext = context;
        this.component = component;
        this.mBaseAppData = baseAppData;
        this.mSpipe = Spipe.inst();
        this.initSharePlatforms(this.mSpipe.mSharePlatforms);
    }

    public final void a(AbsArticle absArticle, long arg6) {
        AlertDialog.Builder builder = com.ss.android.j.b.createDialogBuilder(this.mContext);
        builder.setMessage(com.ss.android.article.browser.R.string.ss_hint_share_when_favor);
        builder.setPositiveButton(com.ss.android.article.browser.R.string.ss_label_share_when_favor_confirm, new p(this, absArticle, arg6));
        builder.setNegativeButton(com.ss.android.article.browser.R.string.ss_label_share_when_favor_cancel, new q(this));
        builder.show();
    }

    private void initSharePlatforms(SharePlatform[] sharePlatforms) {
        ArrayList<SharePlatform> sharePlatformArrayList = new ArrayList(Arrays.asList(sharePlatforms));
        int lastIndex = sharePlatformArrayList.size() - 1;
        while (lastIndex >= 0) {
            if ("qzone_sns".equals(sharePlatformArrayList.get(lastIndex).mName)) {
                sharePlatformArrayList.remove(lastIndex);
            } else {
                --lastIndex;
                continue;
            }
            break;
        }

        this.mSharePlatforms = new SharePlatform[sharePlatformArrayList.size()];
        this.mSharePlatforms = sharePlatformArrayList.toArray(this.mSharePlatforms);
    }

    public final void b(AbsArticle absArticle, long arg16) {
        if ((this.mSpipe.h()) && absArticle != null && this.mSpipe != null && (this.mSpipe.isLogin)) {
            ArrayList v3 = new ArrayList();
            SharePlatform[] v1 = this.mSpipe.mSharePlatforms;
            int v2 = v1.length;
            int v0;
            for (v0 = 0; v0 < v2; ++v0) {
                SharePlatform v4 = v1[v0];
                if ((v4.mLogin) && !"qzone_sns".equals(v4.mName)) {
                    ((List) v3).add(v4.mName);
                }
            }

            if (((List) v3).isEmpty()) {
                return;
            }

            String v8 = "repin";
            if (!NetworkUtils.isNetworkAvailable(this.mContext)) {
                return;
            }

            new com.ss.android.action.a.b.a(this.mContext, this.mWeakHandler, ((List) v3), null, absArticle, arg16, v8, true, 101, 0).start();
            Iterator v1_1 = ((List) v3).iterator();
            while (v1_1.hasNext()) {
                Object v0_1 = v1_1.next();
                if (v0_1 == null) {
                    continue;
                }

                MobClickCombiner.onEvent(this.mContext, "xiangping", (((String) v0_1)) + "_share");
            }
        }
    }

    public final void handleMsg(Message message) {
        int v0;
        int v4 = R.string.ss_send_fail_session_expire;
        int v10 = R.drawable.doneicon_popup_textpage;
        if (this.component.isViewValid()) {
            int v1 = message.arg2 == 101 ? 1 : 0;
            switch (message.what) {
                case 1009: {
                    label_15:
                    if (!this.component.isActive()) {
                        return;
                    }

                    if (this.component != null && (this.component.isActive())) {
                        ArrayList v4_1 = new ArrayList();
                        SharePlatform[] v5 = this.mSharePlatforms;
                        int v6 = v5.length;
                        for (v0 = 0; v0 < v6; ++v0) {
                            SharePlatform v7 = v5[v0];
                            if ((v7.mLogin) && !"qzone_sns".equals(v7.mName)) {
                                v4_1.add(this.mContext.getString(v7.mVerbose));
                            }
                        }

                        if (v1 != 0) {
                        } else if (v4_1.isEmpty()) {
                            UIUtils.showImageToast(this.mContext, v10, R.string.ss_send_success);
                        } else {
                            StringBuffer v5_1 = new StringBuffer();
                            v5_1.append(v4_1.get(0));
                            for (v1 = 1; v1 < v4_1.size(); ++v1) {
                                v5_1.append(this.mContext.getString(com.ss.android.article.browser.R.string.ss_send_success_delimiter));
                                v5_1.append(v4_1.get(v1));
                            }

                            UIUtils.showImageToast(this.mContext, v10, String.format(this.mContext.getString(com.ss.android.article.browser.R.string.ss_send_success_pattern), v5_1));
                        }
                    }

                    Object v0_1 = null;
                    if ((message.obj instanceof Comment)) {
                        v0_1 = message.obj;
                    }

                    if (v0_1 == null) {
                        return;
                    }

                    if (this.mSpipe == null) {
                        return;
                    }

                    if (message.arg1 != 108) {
                        return;
                    }

                    this.mSpipe.a(((Comment) v0_1).t, this.mContext);
                    return;
                }
                case 1010: {
                    label_92:
                    switch (message.arg1) {
                        case 12: {
                            v0 = R.string.ss_send_fail_no_connection;
                            break;
                        }
                        case 14: {
                            v0 = R.string.ss_send_fail_network_timeout;
                            break;
                        }
                        case 15: {
                            v0 = R.string.ss_send_fail_network_error;
                            break;
                        }
                        case 105: {
                            if (this.mSpipe != null) {
                                this.mSpipe.f();
                            }

                            v0 = v4;
                            break;
                        }
                        case 108: {
                            if (this.mSpipe != null) {
                                this.mSpipe.b(this.mContext);
                                if (message.obj != null && ((message.obj instanceof String))) {
                                    this.mSpipe.a((String) message.obj, this.mContext);
                                }
                            }
                            v0 = v4;
                            break;
                        }
                        default: {
                            v0 = R.string.ss_send_fail_unknown;
                            break;
                        }
                    }

                    if (!this.component.isActive()) {
                        return;
                    }

                    if (v1 != 0) {
                        return;
                    }

                    UIUtils.showImageToast(this.mContext, R.drawable.close_popup_textpage, v0);
                    return;
                }
            }
            return;
        }
    }
}

